//
//  WLBindingINTAlertView.m
//  WanLian
//
//  Created by muzhuang on 2018/10/25.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLBindingINTAlertView.h"

@implementation WLBindingINTAlertView

- (instancetype)init
{
    self = [super init];
    if (self) {
        CGFloat width = WIDTH_SCREEN_WL-WLScaleX(72);
        _alert = [[UIView alloc]initWithFrame:CGRectMake(WLScaleX(36), (HEIGHT_SCREEN_WL-WLScaleX(170))/2.0-50, width, WLScaleX(170))];
        _alert.backgroundColor = wlUIColorRGB(0xFAFAFA);
        [self addSubview:_alert];
        _alert.layer.cornerRadius = 5;
        _alert.layer.masksToBounds = YES;
        
        self.frame = [[UIScreen mainScreen]bounds];
        self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, WLScaleX(24), width-10, 24)];
        titleLabel.text = @"绑定INT账户";
        titleLabel.textColor = wlBlackColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [_alert addSubview:titleLabel];
        
        UIView *vTextBack = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(30), WLScaleX(65), width-WLScaleX(60), WLScaleX(40))];
        vTextBack.layer.cornerRadius = 5;
        vTextBack.layer.masksToBounds = YES;
        vTextBack.layer.borderWidth = 1;
        vTextBack.layer.borderColor = wlLightTextColor.CGColor;
        [_alert addSubview:vTextBack];
        
        btnClear = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(vTextBack.frame)-WLScaleX(30), WLScaleX(70), WLScaleX(30), WLScaleX(30))];
        [btnClear setImage:[UIImage imageNamed:@"text_delete"] forState:UIControlStateNormal];
        [btnClear addTarget:self action:@selector(textClearAction) forControlEvents:UIControlEventTouchUpInside];
        [_alert addSubview:btnClear];
        btnClear.hidden = YES;
        
        textAddr = [[UITextView alloc] initWithFrame:CGRectMake(WLScaleX(35), WLScaleX(65), width-WLScaleX(85), WLScaleX(40))];
        textAddr.backgroundColor = [UIColor clearColor];
        textAddr.delegate = self;
        textAddr.returnKeyType = UIReturnKeyDone;
        textAddr.keyboardType = UIKeyboardTypeASCIICapable;
        textAddr.font = [WLCommonTool fontTypePingFangSCRegular:14];
        textAddr.textColor = wlDarkGrayColor;
//        textAddr.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_alert addSubview:textAddr];
        
        UIView *henV = [[UIView alloc]initWithFrame:CGRectMake(0, WLScaleX(170-44), width, 1)];
        henV.backgroundColor = wlUIColorRGB(0xF2F2F2);
        [_alert addSubview:henV];
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, WLScaleX(170-44), width/2.0-20, WLScaleX(44))];
        cancelBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:wlUnderlineColor forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [_alert addSubview:cancelBtn];
        
        UIView *verticaline = [[UIView alloc]initWithFrame:CGRectMake(width/2.0, WLScaleX(170-37), 1, WLScaleX(30))];
        verticaline.backgroundColor = wlUIColorRGB(0xF2F2F2);
        [_alert addSubview:verticaline];
        
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(width/2.0+10, WLScaleX(170-44), width/2.0-20, WLScaleX(44))];
        sureBtn.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:17];
        [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [sureBtn setTitleColor:wlUIColorRGB(0x29A8FA) forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [_alert addSubview:sureBtn];
    }
    return self;
}

- (void)cancelClick
{
    [self hide];
}

- (void)sureClick
{
    NSString *str = textAddr.text;
//    str = @"kdieowkdijfidcnjdicndjcjdncjdn";
    if (![WLCommonTool evalueateINTAccount:str]) {
        return;
    }
    else
    {
        WLWkSelf
        NSDictionary *paramsp = @{@"intAccount":str};//SF678FHHHGYU76BNJ78
        [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"user/modify/bindint" withParamsDic:paramsp success:^(NSDictionary *successDict) {
            [weakSelf hide];
            if (weakSelf.okAction) {
                weakSelf.okAction(str);
            }
            WLUserInfo *user = [[WLUseManager shareUser] getUserInformation];
            user.intAccount = str;
            [[WLUseManager shareUser] updateUserValue:user];
        } failure:^(NSString *errorMsg, NSInteger errorTag) {
            [weakSelf showHUDMessage:errorMsg];
        } error:^(NSError *error) {
            
        } inView:self];
    }
}

- (void)show
{
    if (self.superview==nil) {
        UIWindow *window =  [[UIApplication sharedApplication].windows firstObject];
        [window addSubview:self];
    }
    
    self.alpha = 0;
    _alert.transform = CGAffineTransformScale(_alert.transform,0.1,0.1);
    WLWkSelf
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alert.transform = CGAffineTransformIdentity;
        self.alpha = 1;
    }];
}

- (void)hide
{
    if (self.superview) {
        WLWkSelf
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.alert.transform = CGAffineTransformScale(weakSelf.alert.transform,0.1,0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

- (void)textClearAction
{
    textAddr.text = @"";
    btnClear.hidden = YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length>=42) {
        textView.text = [textView.text substringToIndex:42];
    }
    if (textView.text.length<1) {
        btnClear.hidden = YES;
    }
    else
    {
        btnClear.hidden = NO;
    }
    return;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //如果是删除减少字数，都返回允许修改
    if ([text isEqualToString:@""]) {
        return YES;
    }
    else if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    if (range.location>= 42)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
